package util;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class BlockingThreadPool {

	BlockingQueue<Integer> bqueue;

	public BlockingThreadPool(int capacity) {
		bqueue = new ArrayBlockingQueue<Integer>(capacity, true);
	}

	public void add(Runnable runbl) {
		try {
			bqueue.put(1);
			new Thread(new BlockingThread(runbl, bqueue)).start();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public int remainingCapacity() {
		return bqueue.remainingCapacity();
	}

}

class BlockingThread implements Runnable {

	public BlockingQueue<Integer> bqueue;

	public Runnable task;

	public BlockingThread(Runnable task, BlockingQueue<Integer> bqueue) {
		this.bqueue = bqueue;
		this.task = task;
	}

	public void run() {
		task.run();
		bqueue.poll();
	}
};
